//
//  RABookMallDetailController.m
//  ReadApp
//
//  Created by 飞猪 on 2018/11/7.
//  Copyright © 2018年 Facebook. All rights reserved.
//

#import "RABookMallDetailController.h"
#import "RABookMallCell.h"
#import "RDNetworkTool.h"
#import "RABookDetailController.h"
#import "RCTEntryViewController.h"

@interface RABookMallDetailController () <
UITableViewDataSource,
UITableViewDelegate
>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *models;

@property (nonatomic, strong) RABookMallHeaderView *headerView;

@property (nonatomic, strong) UIView *tableFooterView;

@property (nonatomic, strong) NSMutableArray<RABookFaceModel *> *topModels;

@end

@implementation RABookMallDetailController

- (void)viewDidLoad {
    [super viewDidLoad];
  
  self.view.backgroundColor = [UIColor whiteColor];
  
  [self.view addSubview:self.tableView];
  [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
    make.edges.mas_equalTo(self.view);
  }];
  
  [self requestForChapterList];
  
}

#pragma mark - Network
- (void)requestForChapterList {
  
  NSInteger categorytype = _type + 1;
  
  NSDictionary *params = @{
                           @"id": [@(categorytype) stringValue] ?: @"1"
                           };
  [SVProgressHUD showWithStatus:@"加载中..."];
  [SVProgressHUD setFont:[UIFont boldSystemFontOfSize:14.f]];
  [SVProgressHUD setForegroundColor:[UIColor grayColor]];
  [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
  [SVProgressHUD setDefaultStyle:SVProgressHUDStyleDark];
  [SVProgressHUD setDefaultAnimationType:SVProgressHUDAnimationTypeNative];
  
  [RDNetworkTool requestForBookFaceList:params successBlock:^(NSArray<RABookFaceModel *> *models) {
    [SVProgressHUD dismiss];
    
    if (models.count <= 2) {
      [self.topModels addObjectsFromArray:models];
    }
    else {
      [self.topModels addObjectsFromArray:[models subarrayWithRange:NSMakeRange(0, 2)]];
      NSArray *tempArr = [models subarrayWithRange:NSMakeRange(2, models.count - 2)];
      
      NSMutableArray *temp = [NSMutableArray array];
      for (RABookFaceModel *model in tempArr) {
        if ([model.templet isEqualToString:@"categoryCard"]) {
          [temp addObject:model];
        }
      }
      [self.models addObjectsFromArray:temp.copy];
    }
    
    self.headerView.models = self.topModels.copy;
    [self.tableView reloadData];
    
  } failBlock:^(NSString *errorMsg) {
    [SVProgressHUD dismiss];
    [SVProgressHUD showErrorWithStatus:errorMsg];
  }];
  
}

#pragma mark - Event
- (void)clicked:(RABookMallCellCallBackType)type index:(NSInteger)index cell:(RABookMallCell *)cell {
  
  if (type == RABookMallCellCallBackTypeMore) {
    
    [self.navigationController.tabBarController setSelectedIndex:2];
    
  }
  else if (type == RABookMallCellCallBackTypeDetail) {
    
    RABookDetailController *bookDetailController = [[RABookDetailController alloc] init];
    
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
    RABookFaceModel *model = [self.models objectAtIndex:indexPath.section];
    bookDetailController.model = [model.children objectAtIndex:index];
    [self.navigationController pushViewController:bookDetailController animated:YES];
    
  }
  else {
    
  }
  
}

#pragma mark - Lazyload

- (UITableView *)tableView {
  if (!_tableView) {
    _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    _tableView.backgroundColor = [UIColor whiteColor];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.rowHeight = UITableViewAutomaticDimension;
    _tableView.estimatedRowHeight = 168.f;
    [_tableView registerClass:[RABookMallCell class] forCellReuseIdentifier:@"RABookMallCell"];
    _tableView.tableFooterView = self.tableFooterView;
    _tableView.tableHeaderView = self.headerView;
  }
  return _tableView;
}

- (UIView *)tableFooterView {
  if (!_tableFooterView) {
    _tableFooterView = [UIView new];
    _tableFooterView.backgroundColor = UIColorHex(eeeeee);
    _tableFooterView.frame = CGRectMake(0, 0, 0, 48);
    
    UILabel *lab = [UILabel new];
    lab.text = @"已经到底了，到别的频道看看吧！";
    lab.textColor = UIColorHex(999999);
    lab.font = [UIFont systemFontOfSize:12.f];
    [_tableFooterView addSubview:lab];
    [lab mas_makeConstraints:^(MASConstraintMaker *make) {
      make.center.mas_equalTo(0);
    }];
  }
  return _tableFooterView;
}

- (NSMutableArray *)models {
  if (!_models) {
    _models = [NSMutableArray array];
  }
  return _models;
}

- (NSMutableArray<RABookFaceModel *> *)topModels {
  if (!_topModels) {
    _topModels = [NSMutableArray array];
  }
  return _topModels;
}

- (RABookMallHeaderView *)headerView {
  if (!_headerView) {
    _headerView = [[RABookMallHeaderView alloc] init];
    _headerView.type = _type;
    _headerView.frame = CGRectMake(0, 0, 0, kScreenWidth * 0.6 + 88.f);
    __weak typeof(RABookMallDetailController *) ws = self;
    _headerView.clickedCallback = ^(RABookMallHeaderItemModel *model) {
      [ws jumpToCategory:model];
    };
    [_headerView setJumpToBookDetail:^(RABookFaceModel *model) {
      RABookDetailController *ctrl = [[RABookDetailController alloc] init];
      ctrl.model = model;
      [ws.navigationController pushViewController:ctrl animated:YES];
    }];
  }
  return _headerView;
}

- (void)jumpToCategory:(RABookMallHeaderItemModel *)model {
  NSLog(@"%@", model);
  
  if ([model.jId isEqualToString:@"0"]) {
    
    [self.navigationController.tabBarController setSelectedIndex:2];
  }
  else {
    RCTEntryViewController *ctrl = [[RCTEntryViewController alloc] init];
    ctrl.searchSubCategoryId = model.jId;
    ctrl.categoryShowBack = @"1";
    [self.navigationController pushViewController:ctrl animated:YES];
  }
  
}


#pragma mark - UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  return self.models.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  
  RABookMallCell *cell = [tableView dequeueReusableCellWithIdentifier:@"RABookMallCell" forIndexPath:indexPath];
  
  RABookFaceModel *model = self.models[indexPath.section];
  cell.model = model;
  [cell setClickedCallback:^(RABookMallCell *cell, RABookMallCellCallBackType type, NSInteger index) {
    [self clicked:type index:index cell:cell];
  }];
  
  return cell;
}

#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//  [tableView deselectRowAtIndexPath:indexPath animated:YES];
//  
//  RABookFaceModel *model = self.models[indexPath.row];
//  
//  //  [self.navigationController pushViewController:[UIViewController new] animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
  return 10.f;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
  UIView *v = [UIView new];
  v.backgroundColor = UIColorHex(eeeeee);
  return v;
}

@end
